//
//  UIButton+Category.m
//  DCProject
//
//  Created by bigbing on 2019/4/1.
//  Copyright © 2019 bigbing. All rights reserved.
//

#import "UIButton+Category.h"

@implementation UIButton (Category)


#pragma mark -  图片在左  文字在右  spacing间隔
- (void)dc_buttonIconLeftWithSpacing:(CGFloat)spacing
{
    [self setTitleEdgeInsets:UIEdgeInsetsMake(0, spacing/2, 0, -spacing/2)];
    [self setImageEdgeInsets:UIEdgeInsetsMake(0, -spacing/2, 0, spacing/2)];
}


#pragma mark -  图片在右  文字在左  spacing间隔
- (void)dc_buttonIconRightWithSpacing:(CGFloat)spacing
{
    [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -self.imageView.bounds.size.width, 0, self.imageView.bounds.size.width)];
    [self setImageEdgeInsets:UIEdgeInsetsMake(0, self.titleLabel.bounds.size.width + spacing, 0, -self.titleLabel.bounds.size.width - spacing)];
}


#pragma mark -  图片在上  文字在下  spacing间隔
- (void)dc_buttonIconTopWithSpacing:(CGFloat)spacing
{
    [self setImageEdgeInsets:UIEdgeInsetsMake(-spacing, (self.frame.size.width - self.imageView.frame.size.width)/2, 0, (self.frame.size.width - self.imageView.frame.size.width)/2)];
    [self setTitleEdgeInsets:UIEdgeInsetsMake(self.imageView.frame.size.height + spacing, -self.imageView.frame.size.width, 0, 0)];
}


#pragma mark -  图片在下  文字在上  spacing间隔
- (void)dc_buttonIconBottomWithSpacing:(CGFloat)spacing
{
    
}


#pragma mark - 边框  切角
- (void)dc_layerBorderWith:(CGFloat)width color:(UIColor *)color radius:(CGFloat)radius
{
    if (width && width > 0) {
        self.layer.borderWidth = width;
        self.layer.borderColor = color.CGColor;
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = radius;
    }else{
        self.clipsToBounds = YES;
        self.layer.cornerRadius = radius;
    }
}

#pragma mark - 简化设置

+(UIButton *)dc_init{
    UIButton * view = UIButton.alloc.init;
    return view;
}

-(UIButton * _Nonnull (^)(NSString * _Nonnull str, UIControlState state))dc_title{
    return ^(NSString * str,UIControlState state){
        [self setTitle:str forState:state];
        return self;
    };
}
-(UIButton * _Nonnull (^)(UIColor * _Nonnull, UIControlState))dc_titleColor{
    return ^(UIColor * color,UIControlState state){
        [self setTitleColor:color forState:state];
        return self;
    };
}
-(UIButton * _Nonnull (^)(NSString * _Nonnull, UIControlState))dc_titleColorHex{
    return ^(NSString * str,UIControlState state){
        
        NSString *cString = [[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
        
        // String should be 6 or 8 characters
        if ([cString length] > 6) {
            // 判断前缀
            if ([cString hasPrefix:@"0X"])
                cString = [cString substringFromIndex:2];
            if ([cString hasPrefix:@"#"])
                cString = [cString substringFromIndex:1];
            if ([cString length] != 6)
                return self;
            // 从六位数值中找到RGB对应的位数并转换
            NSRange range;
            range.location = 0;
            range.length = 2;
            //R、G、B
            NSString *rString = [cString substringWithRange:range];
            range.location = 2;
            NSString *gString = [cString substringWithRange:range];
            range.location = 4;
            NSString *bString = [cString substringWithRange:range];
            // Scan values
            unsigned int r, g, b;
            [[NSScanner scannerWithString:rString] scanHexInt:&r];
            [[NSScanner scannerWithString:gString] scanHexInt:&g];
            [[NSScanner scannerWithString:bString] scanHexInt:&b];
            
            [self setTitleColor:[UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0] forState:state];
        }
        return self;
    };
}
-(UIButton * _Nonnull (^)(UIFont * _Nonnull))dc_titleFont{
    return ^(UIFont * font){
        self.titleLabel.font = font;
        return self;
    };
}
- (UIButton * _Nonnull (^)(CGFloat))dc_titleFontSize{
    return ^(CGFloat floatValue){
        self.titleLabel.font = [UIFont systemFontOfSize:floatValue weight:UIFontWeightRegular];
        return self;
    };
}
- (UIButton * _Nonnull (^)(CGFloat))dc_titleFontSizeMedium{
    return ^(CGFloat floatValue){
        self.titleLabel.font = [UIFont systemFontOfSize:floatValue weight:UIFontWeightMedium];
        return self;
    };
}
- (UIButton * _Nonnull (^)(CGFloat))dc_titleFontSizeSemibold{
    return ^(CGFloat floatValue){
        self.titleLabel.font = [UIFont systemFontOfSize:floatValue weight:UIFontWeightSemibold];
        return self;
    };
}
-(UIButton * _Nonnull (^)(CGFloat))dc_titleFontSizeBold{
    return ^(CGFloat floatValue){
        self.titleLabel.font = [UIFont systemFontOfSize:floatValue weight:UIFontWeightBold];
        return self;
    };
}
- (UIButton * _Nonnull (^)(UIImage * _Nonnull, UIControlState))dc_image{
    return ^(UIImage * img,UIControlState state){
        [self setImage:img forState:state];
        return self;
    };
}
- (UIButton * _Nonnull (^)(UIEdgeInsets))dc_imageEdgeInsets{
    return ^(UIEdgeInsets edge){
        self.imageEdgeInsets = edge;
        return self;
    };
}

- (UIButton * _Nonnull (^)(UIEdgeInsets))dc_titleEdgeInsets{
    return ^(UIEdgeInsets edge){
        self.titleEdgeInsets = edge;
        return self;
    };
}
- (UIButton * _Nonnull (^)(UIEdgeInsets))dc_contentEdgeInsets{
    return ^(UIEdgeInsets edge){
        self.contentEdgeInsets = edge;
        return self;
    };
}



@end
